package com.ldc.wandroid.ui.fragments;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.ldc.common.config.ArgParams;
import com.ldc.common.model.LoginInfoModel;
import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.SPUtils;
import com.ldc.common.utils.StringUtils;
import com.ldc.common.utils.ToastUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.common.utils.ViewUtils;
import com.ldc.wandroid.R;
import com.ldc.wandroid.databinding.FragmentLoginBinding;
import com.ldc.wandroid.ui.activitys.MainActivity;
import com.ldc.wandroid.viewmodel.LoginViewModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseMvvmFragment<FragmentLoginBinding, LoginViewModel> {
    private String userName;
    private String passWord;

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getLoginInfoModel().removeObserver(this::LoginData);
    }

    @Override
    protected FragmentLoginBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentLoginBinding.inflate(inflater, container, false);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showContentView();
        initTopBar();
        viewModel.getLoginInfoModel().observeForever(this::LoginData);
        viewBinding.btnLoginRegister.setOnClickListener(view -> {
            login();
        });
        viewBinding.tvRegister.setOnClickListener(view -> {
            Navigation.findNavController(view)
                    .navigate(R.id.registerFragment);
        });
    }

    private void initTopBar() {
        viewBinding.topBar.tvTitle.setText(getString(R.string.str_login));
        viewBinding.topBar.lineMore.setVisibility(View.GONE);
        viewBinding.topBar.lineBack.setOnClickListener(view -> {
            Navigation.findNavController(view)
                    .navigateUp();
        });
    }

    @Override
    protected LoginViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, LoginViewModel.class);
    }


    private void LoginData(LoginInfoModel model) {
        viewBinding.stateView.showContentView();
        if (null == model) {
            return;
        }
        SPUtils.getInstance().putString(ArgParams.USER_NAME, userName);
        SPUtils.getInstance().putString(ArgParams.USER_PASSWORD, passWord);
        MainActivity.start(requireContext());
        requireActivity().finish();

    }


    private void login() {
        userName = ViewUtils.getText(this.getView(), R.id.et_user_name);
        passWord = ViewUtils.getText(this.getView(), R.id.et_user_password);
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passWord)) {
            ToastUtils.showShort(getString(R.string.str_please_check_if_the_data_is_legitimate));
            return;
        }
        viewBinding.stateView.showLoadingView();
        viewModel.login(userName, passWord);
    }


}
